﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;
using System.Media;
using System.Configuration;
using System.Collections;
using System.CodeDom;
using System.Collections.ObjectModel;

namespace EOffice.Business
{
    class cUserOnline
    {
        private Thread thrDeleteActivty;
        public event EventHandler evnNotifyDelete;
        private String sLocalUser;
        private ArrayList ouarraylist = new ArrayList();
        private cUser[] OnlineUserArray;
        private int counter = 0;

        public cUserOnline(IPEndPoint localipendpoint, string localusername)
        {
            this.sLocalUser = localusername;
            OnlineUserArray = new cUser[2];
            thrDeleteActivty = new Thread(new ThreadStart(DeleteActivity));
            thrDeleteActivty.IsBackground = true;
            thrDeleteActivty.Start();           
        }

        public cUserOnline()
        {
            throw new System.NotImplementedException();
        }

        public int GetMessageNumber(int i)
        {
            return OnlineUserArray[i].GetCMessageNumber();
        }

        public void AddNewUser(IPEndPoint newuser, string username, string NumberMessage)
        {
            if (CheckNew(newuser, username))
            {
                OnlineUserArray[counter++] = new cUser(newuser, username, Convert.ToInt32(NumberMessage));
                Array.Resize(ref OnlineUserArray, counter + 5);
            }
            else
                OnlineUserArray[CheckNewNum(newuser, username)].setActivity(1);
        }

        public Boolean CheckNew(IPEndPoint newuser, string username)
        {
            Boolean ret = true;
            for (int i = 0; i < counter; i++)
            {
                if ((OnlineUserArray[i].getUserName().Equals(username) && OnlineUserArray[i].getUserIp().Equals(newuser)) || sLocalUser.Equals(username))
                    ret = false;
            }
            return ret;
        }

        public int CheckWithIp(IPEndPoint ip)
        {
            int m = 0;
            for (int i = 0; i < counter; i++)
            {
                if (OnlineUserArray[i].getUserIp().Equals(ip))
                    m = i;
            }
            return m;
        }

        public int CheckWithName(string name)
        {
            int m = 0;
            for (int i = 0; i < counter; i++)
            {
                if (OnlineUserArray[i].getUserName().Equals(name))
                    m = i;
            }
            return m;
        }

        public string FindUserName(IPEndPoint ip)
        {
            return OnlineUserArray[CheckWithIp(ip)].getUserName();
        }

        public IPEndPoint FindUserIp(String name)
        {
            return OnlineUserArray[CheckWithName(name)].getUserIp();
        }

        public int CheckNewNum(IPEndPoint newuser, string username)
        {
            int m = 0;
            for (int i = 0; i < counter; i++)
            {
                if (OnlineUserArray[i].getUserName().Equals(username) && OnlineUserArray[i].getUserIp().Equals(newuser))
                    m = i;
            }
            return m;
        }

        public int CheckNewNum(IPEndPoint newuser)
        {
            int m = 0;
            for (int i = 01; i < counter; i++)
            {
                if (OnlineUserArray[i].getUserIp().Equals(newuser))
                    m = i;
            }
            return m;
        }

        protected void OnNotifyDelete(string mes)
        {
            if (evnNotifyDelete != null) evnNotifyDelete(this, new NotifyDeleteEventArgs(mes));
        }

        public int GetNumberOfMessage(int m) { return OnlineUserArray[m].GetCMessageNumber(); }

        public void SetMessageNumber(int m) { OnlineUserArray[m].SetCMessageNumber(); }

        public int GetPassive(int i) { return OnlineUserArray[i].GetPassive(); }

        public void DeleteActivity()
        {
            while (true)
            {
                Thread.Sleep(20000);
                for (int i = 1; i < counter; i++)
                {
                    OnlineUserArray[i].setActivity(0);
                }
                Thread.Sleep(20000);
                DeleteUser();
            }
        }

        public void MakeActive(int i)
        {
            OnlineUserArray[i].setActivity(1);
            OnlineUserArray[i].MakeActive();
        }

        public void DeleteUser()
        {
            for (int i = 1; i < counter; i++)
            {
                if (OnlineUserArray[i].getActivity() == 0)
                {
                    if (OnlineUserArray[i].GetPassive() == 0)
                    {
                        DeleteUserPermanently(OnlineUserArray[i].getUserName(), OnlineUserArray[i].getUserIp());
                        this.OnNotifyDelete(OnlineUserArray[i].getUserName());
                    }
                    OnlineUserArray[i].MakePassive();
                }
            }
        }

        public int GetCount()
        {
            return counter;
        }
        public cUser[] GetOnlineUser()
        {
            return OnlineUserArray;
        }
        public Boolean CheckNumberOfMessage(string a, IPEndPoint ip)
        {

            if (OnlineUserArray[CheckWithIp(ip)].GetCMessageNumber() % 100 == Convert.ToInt32(a) % 100)
                return true;
            else
                return false;

        }

        public void DeleteUserPermanently(string username, IPEndPoint ip)
        {
            for (int i = 1; i < counter; i++)
            {
                if (OnlineUserArray[i].getUserName().Equals(username) && OnlineUserArray[i].getUserIp().Equals(ip))
                {
                    cUser tempuser = OnlineUserArray[counter - 1];
                    OnlineUserArray[counter - 1] = OnlineUserArray[i];
                    OnlineUserArray[i] = tempuser;
                    counter--;
                    Array.Resize(ref OnlineUserArray, counter + 5);
                }
            }

        }
    }
//
//Class user
//
    class cUser
    {
        private IPEndPoint IPUser;
        private string username;
        private int ActivateNum;
        private int PassiveNum = 1;
        private int CMessageNum;
        private int PMessageNum;
        private int PrivateRoomState = 0;

        public cUser(IPEndPoint ip, string username, int m)
        {
            IPUser = ip;
            this.username = username;
            ActivateNum = 1;
            this.CMessageNum = m;
        }

        public void setUserIp(IPEndPoint user)
        {
            IPUser = user;
        }

        public int GetPrivateRoomState()
        {
            return this.PrivateRoomState;
        }

        public void OpenPrivateRoom(int i)
        {
            this.PrivateRoomState = 1;
        }

        public void setUserName(string username)
        {
            this.username = username;
        }

        public void setActivity(int activate)
        {
            this.ActivateNum = activate;
        }

        public IPEndPoint getUserIp()
        {
            return IPUser;
        }

        public string getUserName()
        {
            return username;
        }

        public int getActivity()
        {
            return ActivateNum;
        }

        public int GetCMessageNumber()
        {
            // common message number
            return CMessageNum;
        }

        public void SetPMessageNumber()
        {
            //private message number
            PMessageNum++;
        }

        public void Color()
        {

        }

        public int GetPMessageNumber()
        {
            return PMessageNum;
        }

        public void SetCMessageNumber()
        {
            ++CMessageNum;
        }

        public IPAddress getIpAdress()
        {
            return IPUser.Address;
        }

        public void MakePassive()
        {
            PassiveNum = 0;
        }

        public int GetPassive()
        {
            return PassiveNum;
        }

        public void MakeActive()
        {
            PassiveNum = 1;
        }
    }

//
//Classes event CNotify
//
    public class NotifyBecomeOnlineEventArgs : System.EventArgs
    {
        public readonly string Message;
        public NotifyBecomeOnlineEventArgs(string message)
        {
            Message = message;
        }
    }

    public class NotifyDaddyEventArgs : System.EventArgs
    {
        public readonly string Message;
        public NotifyDaddyEventArgs(string message)
        {
            Message = message;
        }
    }

    public class NotifyDeleteEventArgs : System.EventArgs
    {
        public readonly string Message;
        public NotifyDeleteEventArgs(string message)
        {
            Message = message;
        }
    }

    public class NotifyEnterEventArgs : System.EventArgs
    {
        public readonly string Message;
        public NotifyEnterEventArgs(string message)
        {
            Message = message;
        }
    }
    public class NotifyPrivateEventArgs : System.EventArgs
    {
        public readonly string name;
        public NotifyPrivateEventArgs(string name)
        {
            this.name = name;

        }
    }
    public class NotifyPrivateSendEventArgs : System.EventArgs
    {
        public readonly string Message;
        public NotifyPrivateSendEventArgs(string message)
        {
            Message = message;
        }
    }
}
